package com.ruoyi.wzgl.ckgl.ckgl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wzgl.ckgl.ckgl.domain.Ckgl;
import com.ruoyi.wzgl.ckgl.ckgl.service.ICkglService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 仓库管理Controller
 *
 * @author huacai
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/wzgl/ckgl")
public class CkglController extends BaseController {
    @Resource
    private ICkglService ckglService;

    /**
     * 查询仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:ckgl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ckgl ckgl) {
        startPage();
        List<Ckgl> list = ckglService.selectCkglList(ckgl);
        return getDataTable(list);
    }

    /**
     * 导出仓库管理列表
     */
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ckgl ckgl) {
        List<Ckgl> list = ckglService.selectCkglList(ckgl);
        ExcelUtil<Ckgl> util = new ExcelUtil<Ckgl>(Ckgl.class);
        util.exportExcel(response, list, "仓库管理数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Ckgl> util = new ExcelUtil<Ckgl>(Ckgl.class);
        util.importTemplateExcel(response, "仓库管理数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "仓库管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wzgl:ckgl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Ckgl> util = new ExcelUtil<Ckgl>(Ckgl.class);
        InputStream inputStream = file.getInputStream();
        List<Ckgl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = ckglService.batchInsertCkgl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取仓库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:ckgl:query')")
    @GetMapping(value = "/{ckglId}")
    public AjaxResult getInfo(@PathVariable("ckglId") Integer ckglId) {
        return success(ckglService.selectCkglByCkglId(ckglId));
    }

    /**
     * 新增仓库管理
     */
    @PreAuthorize("@ss.hasPermi('wzgl:ckgl:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ckgl ckgl) {
        ckgl.setUserId(Math.toIntExact(getUserId())); //用户ID
        ckgl.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        ckgl.setCreateBy(getUsername()); //用户名
        return toAjax(ckglService.insertCkgl(ckgl));
    }

    /**
     * 修改仓库管理
     */
    @PreAuthorize("@ss.hasPermi('wzgl:ckgl:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ckgl ckgl) {
        ckgl.setUpdateBy(getUsername());
        return toAjax(ckglService.updateCkgl(ckgl));
    }

    /**
     * 删除仓库管理
     */
    @PreAuthorize("@ss.hasPermi('wzgl:ckgl:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ckglIds}")
    public AjaxResult remove(@PathVariable Integer[] ckglIds) {
        return toAjax(ckglService.deleteCkglByCkglIds(ckglIds));
    }
}
